using Godot;

public class ItemEntity : Entity
{
    public Vector2 velocity = new Vector2();
    public bool TargetMode = false;
    public KinematicBody2D Target;
    public AnimationPlayer ap;
    public KinematicBody2D kinematicBody2D;
    public float cooldown = 1;
    public override void _Ready()
    {
        kinematicBody2D = GetNode<KinematicBody2D>("KinematicBody2D");
        ap = GetNode<AnimationPlayer>("AnimationPlayer");
        ap.Play("normal");
    }

    public override void _PhysicsProcess(float delta)
    {
        if (cooldown > 0f)
        {
            cooldown -= delta;
        }
        if (TargetMode)
        {
            Vector2 velocity = this.kinematicBody2D.GlobalPosition.DirectionTo(Target.GlobalPosition) * 128;
            velocity = kinematicBody2D.MoveAndSlide(velocity);

            if (
                this.kinematicBody2D.GlobalPosition.x >= Target.GlobalPosition.x - 8f &&
                this.kinematicBody2D.GlobalPosition.x <= Target.GlobalPosition.x + 8f &&
                this.kinematicBody2D.GlobalPosition.y >= Target.GlobalPosition.y - 8f &&
                this.kinematicBody2D.GlobalPosition.y <= Target.GlobalPosition.y + 8f && cooldown <= 0f
                )
            {
                QueueFree();
            }
        }
        else
        {
            velocity.y += 16 * delta;
            velocity = kinematicBody2D.MoveAndSlide(velocity, Vector2.Up);
        }
    }
    public void EntityEntered(KinematicBody2D body)
    {
        if (body.Name == "Player")
        {
            GD.Print(body.Name);
            TargetMode = true;
            Target = body;
        }
        else
        {
            GD.Print(body.Name);
        }
    }

    public void EntityEixt(KinematicBody2D body)
    {
        if (body.Name == "Player")
        {
            TargetMode = false;
        }
    }
}
